home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Sequence Grabber / Example Video Panel / ExampleVideoPanelTester.c < prev    next >
Encoding:
Text File  |  1992-10-18  |  10.6 KB  |  583 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        ExampleVideoPanelTester.c
  3.  
  4.     Contains:    Example video panel component tester routines.
  5.  
  6.     Written by:    Gary Woodcock
  7.  
  8.     Copyright:    © 1992 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12. */
  13.  
  14. //-----------------------------------------------------------------------
  15. // Includes
  16.  
  17. #include "ExampleVideoPanelPrivate.h"
  18. #include <Menus.h>
  19. #include <Windows.h>
  20. #include <QuickDraw.h>
  21. #include <OSEvents.h>
  22. #include <Resources.h>
  23. #include <Desk.h>
  24. #include <Fonts.h>
  25. #include <ToolUtils.h>
  26.  
  27. #ifndef THINK_C
  28.     #include <Packages.h>
  29. #endif THINK_C
  30.  
  31. // Make sure the debug flag in this file is set appropriately
  32. #include "DebugFlags.h"    
  33.  
  34. //-----------------------------------------------------------------------
  35. // Constants
  36.  
  37. // Menu bar
  38. enum
  39. {
  40.     kMenuBarID = 128
  41. };
  42.  
  43. // Menus
  44. enum
  45. {
  46.     kAppleID = 128,
  47.     kFileID,
  48.     kEditID
  49. };
  50.  
  51. // Apple menu items
  52. enum
  53. {
  54.     kAboutItem = 1
  55. };
  56.  
  57. // File menu items
  58. enum
  59. {
  60.     kVideoSettings = 1,
  61.     kQuitItem = 3
  62. };
  63.  
  64. // Edit menu items
  65. enum
  66. {
  67.     kUndoItem = 1,
  68.     kCutItem = 3,
  69.     kCopyItem,
  70.     kPasteItem,
  71.     kClearItem
  72. };
  73.  
  74. enum
  75. {
  76.     kAboutDialogID = 128
  77. };
  78.  
  79. // Common DITL items
  80. enum
  81. {
  82.     kAboutOKButton = 1,
  83.     kAboutOKButtonOutline
  84. };
  85.  
  86. //-----------------------------------------------------------------------
  87. // Globals
  88.  
  89. MenuHandle            gAppleMenu;
  90. MenuHandle            gFileMenu;
  91. MenuHandle            gEditMenu;
  92. EventRecord            gTheEvent;
  93. Component            gExampleVideoPanelComponentID;
  94. GrafPtr                gSavedPort;
  95. Boolean                gQuitFlag;
  96. SeqGrabComponent    gSeqGrabber;
  97. SGChannel            gVideoChannel;
  98. WindowPtr            gMonitor;
  99.  
  100. //-----------------------------------------------------------------------
  101. // Prototypes
  102.  
  103. static void
  104. DoInit (void);
  105.  
  106. static void
  107. DoMenuSetup (void);
  108.  
  109. static void
  110. HandleEvent (void);
  111.  
  112. static void
  113. HandleMouseDown    (void);
  114.  
  115. static void
  116. AdjustMenus (void);
  117.  
  118. static void
  119. Enable (Handle menu, short item, Boolean ok);
  120.  
  121. static void
  122. HandleMenu (long menu);
  123.  
  124. static void
  125. DoAboutDialog (void);
  126.  
  127. static void
  128. DoQuit (void);
  129.  
  130. pascal void
  131. TesterDrawProc (DialogPtr theDialog, short theItemNum);
  132.  
  133. extern void
  134. RegisterExampleVideoPanel (void);
  135.  
  136. //-----------------------------------------------------------------------
  137.  
  138. main (void)
  139. {
  140.     // Init
  141.     DoInit();
  142.     DoMenuSetup();
  143.     
  144.     // Eat events until done
  145.     do
  146.     {
  147.         HandleEvent();
  148.     }
  149.     while (!gQuitFlag);
  150.     
  151.     // Take off, eh?
  152.     ExitToShell();
  153. }
  154.  
  155. //-----------------------------------------------------------------------
  156.  
  157. static void
  158. DoInit (void)
  159. {
  160.     ComponentDescription    theDesc;
  161.     ComponentResult            result = noErr;
  162.     Component                sgCompID = 0L;
  163.     
  164.     // Set up quit flag
  165.     gQuitFlag = false;
  166.     
  167.     // MacMantra™
  168.     MaxApplZone();
  169.     InitGraf (&qd.thePort);
  170.     InitFonts();
  171.     FlushEvents (everyEvent, 0);
  172.     InitWindows();
  173.     InitMenus();
  174.     TEInit();
  175.     InitDialogs (0L);
  176.     InitCursor();
  177.     MoreMasters();
  178.     MoreMasters();
  179.     MoreMasters();
  180.     MoreMasters();
  181.     
  182.     // Register example video panel component
  183.     #ifdef DEBUG_IT
  184.         RegisterExampleVideoPanel();
  185.     #endif 
  186.     
  187.     // Find the example video panel component
  188.       theDesc.componentType = SeqGrabPanelType;
  189.       theDesc.componentSubType = 'vide';
  190.       theDesc.componentManufacturer = 'xmpl';
  191.       theDesc.componentFlags = 0L;
  192.       theDesc.componentFlagsMask = 0L;
  193.     gExampleVideoPanelComponentID = FindNextComponent (nil, &theDesc);
  194.     
  195.     // Find and open a sequence grabber
  196.     theDesc.componentType = SeqGrabComponentType;
  197.     theDesc.componentSubType = 0L;
  198.     theDesc.componentManufacturer = 'appl';
  199.     theDesc.componentFlags = 0L;
  200.     theDesc.componentFlagsMask = 0L;    
  201.     sgCompID = FindNextComponent (nil, &theDesc);
  202.     if (sgCompID != 0L)
  203.     {
  204.         gSeqGrabber = OpenComponent (sgCompID);
  205.     }
  206.     
  207.     // If we got a sequence grabber, set it up
  208.     if (gSeqGrabber != 0L)
  209.     {
  210.         Rect    boundsRect = {60, 20, 180, 180};
  211.         
  212.         // Hope you do better error checking than me!
  213.         gMonitor = NewCWindow (nil, &boundsRect, "\pMonitor", true, movableDBoxProc,
  214.             (WindowPtr) -1L, false, 0L);
  215.         if (gMonitor != nil)
  216.         {
  217.             result = SGInitialize (gSeqGrabber);
  218.             if (result == noErr)
  219.             {
  220.                 result = SGSetGWorld (gSeqGrabber, (CGrafPtr) gMonitor, nil);
  221.                 if (result == noErr)
  222.                 {
  223.                     result = SGNewChannel (gSeqGrabber, VideoMediaType, &gVideoChannel);
  224.                     if ((gVideoChannel != nil) && (result == noErr))
  225.                     {
  226.                         result = SGSetChannelUsage (gVideoChannel, seqGrabPreview);
  227.                         boundsRect = gMonitor->portRect;
  228.                         result = SGSetChannelBounds (gVideoChannel, &boundsRect);
  229.                     }
  230.                     result = SGPrepare (gSeqGrabber, true, false);
  231.                     result = SGStartPreview (gSeqGrabber);
  232.                 }
  233.             }
  234.         }
  235.         else
  236.         {
  237.             DoQuit();
  238.         }
  239.     }
  240. }
  241.  
  242. //-----------------------------------------------------------------------
  243.  
  244. static void
  245. DoMenuSetup (void)
  246. {    
  247.     Handle    theMenuBar = GetNewMBar (kMenuBarID);
  248.     
  249.     // Set up our menus
  250.     SetMenuBar (theMenuBar);
  251.     gAppleMenu = GetMHandle (kAppleID);
  252.     gFileMenu = GetMHandle (kFileID);
  253.     gEditMenu = GetMHandle (kEditID);
  254.     AddResMenu (gAppleMenu, 'DRVR');
  255.     DrawMenuBar();
  256. }
  257.  
  258. //-----------------------------------------------------------------------
  259.  
  260. static void
  261. HandleEvent (void)
  262. {
  263.     ComponentResult    result = noErr;
  264.  
  265.     // Do system stuff
  266.     HiliteMenu (0);
  267.     SystemTask();
  268.     
  269.     // Give some time to the sequence grabber
  270.     if (gSeqGrabber != 0L)
  271.         result = SGIdle (gSeqGrabber);
  272.     
  273.     // Suck an event
  274.     if (WaitNextEvent (everyEvent, &gTheEvent, 0, 0))
  275.     {
  276.         // What was it?
  277.         switch (gTheEvent.what)
  278.         {
  279.             case mouseDown:
  280.             {
  281.                 HandleMouseDown();
  282.                 break;
  283.             }
  284.             case keyDown:
  285.             case autoKey:
  286.             {
  287.                 char    theChar = gTheEvent.message & charCodeMask;
  288.                 long    theMenu = MenuKey (theChar);
  289.  
  290.                 HandleMenu (theMenu);                
  291.                 break;
  292.             }
  293.             default:    // We don't really care about any other events
  294.             {
  295.                 break;
  296.             }
  297.         }
  298.     }
  299. }
  300.  
  301. //-----------------------------------------------------------------------
  302.  
  303. static void
  304. HandleMouseDown (void)
  305. {    
  306.     WindowPtr    theWindow;
  307.     short        windowCode = FindWindow (gTheEvent.where, &theWindow);
  308.     
  309.     // Where was the mouse down?
  310.     switch (windowCode)
  311.     {
  312.         case inSysWindow:
  313.         { 
  314.             SystemClick (&gTheEvent, theWindow);
  315.             break;
  316.         }
  317.         case inMenuBar:
  318.         {
  319.             AdjustMenus();
  320.             HandleMenu (0L);
  321.             break;
  322.         }
  323.         case inDrag:
  324.         {
  325.             // Was it the monitor?
  326.             if (theWindow == gMonitor)
  327.             {
  328.                 Rect            limitRect;
  329.                 RgnHandle        grayRgn = GetGrayRgn();
  330.                 ComponentResult    result = noErr;
  331.                 
  332.                 // Pause the sequence grabber
  333.                 result = SGPause (gSeqGrabber, true);
  334.                 
  335.                 // Find bounds
  336.                 if (grayRgn != nil)
  337.                 {
  338.                     limitRect = (*grayRgn)->rgnBBox;
  339.                 }
  340.                 else
  341.                 {
  342.                     limitRect = qd.screenBits.bounds;
  343.                 }
  344.                 
  345.                 // Drag it
  346.                 DragWindow (theWindow, gTheEvent.where, &limitRect);
  347.                 
  348.                 // Start up the sequence grabber
  349.                 result = SGPause (gSeqGrabber, false);
  350.             }
  351.             break;
  352.         }
  353.         case inGoAway:
  354.         default:
  355.         {
  356.             break;
  357.         }
  358.     }
  359. }
  360.  
  361. //-----------------------------------------------------------------------
  362.  
  363. static void
  364. AdjustMenus (void)
  365. {
  366.     register WindowPeek        wp = nil;
  367.     short                    kind = 0;
  368.     Boolean                    DA = false;
  369.     ComponentResult            result = noErr;
  370.     
  371.     // What kind of window is frontmost?
  372.     wp = (WindowPeek) FrontWindow();
  373.     kind = wp ? wp->windowKind : 0;
  374.     DA = kind < 0;
  375.     
  376.     // Set our menu item states appropriately
  377.     
  378.     // Apple menu
  379.     Enable ((Handle) gAppleMenu, kAboutItem, true);    
  380.     
  381.     // File menu
  382.     Enable ((Handle) gFileMenu, kVideoSettings, true);
  383.     Enable ((Handle) gFileMenu, kQuitItem, true);
  384.  
  385.     // Edit menu
  386.     Enable ((Handle) gEditMenu, 1, DA);
  387.     Enable ((Handle) gEditMenu, 3, DA);
  388.     Enable ((Handle) gEditMenu, 4, DA);
  389.     Enable ((Handle) gEditMenu, 5, DA);
  390.     Enable ((Handle) gEditMenu, 6, DA);
  391.     
  392.     DrawMenuBar();
  393. }
  394.  
  395. //-----------------------------------------------------------------------
  396.  
  397. static void
  398. Enable (Handle menu, short item, Boolean ok)
  399. {
  400.     // Utility routine to enable and disable menu items
  401.     if (ok)
  402.     {
  403.         EnableItem ((MenuHandle) menu, item);
  404.     }
  405.     else
  406.     {
  407.         DisableItem ((MenuHandle) menu, item);
  408.     }
  409. }
  410.  
  411. //-----------------------------------------------------------------------
  412.  
  413. static void
  414. HandleMenu (long theMenu)
  415. {    
  416.     long            mSelect;
  417.     short            menuID;
  418.     short            menuItem;
  419.     ComponentResult    result = noErr;    
  420.     Str255            menuItemStr;
  421.     
  422.     // Did we get a menu?
  423.     if (theMenu == 0L)
  424.     {
  425.         // Nope, get it from menu select
  426.         mSelect = MenuSelect (gTheEvent.where);
  427.     }
  428.     else
  429.     {
  430.         // Yep, use it
  431.         mSelect = theMenu;
  432.     }
  433.     
  434.     // Decode it
  435.     menuID = HiWord (mSelect);
  436.     menuItem = LoWord (mSelect);
  437.     
  438.     // Which menu is it?
  439.     switch (menuID)
  440.     {
  441.         case kAppleID:
  442.         {
  443.             if (menuItem == kAboutItem)
  444.             {
  445.                 DoAboutDialog();
  446.             }
  447.             else    // It's a DA
  448.             {
  449.                 Str255    name;
  450.                 
  451.                 // Open the DA
  452.                 GetPort (&gSavedPort);
  453.                 GetItem (gAppleMenu, menuItem, name);
  454.                 OpenDeskAcc (name);
  455.                 SetPort (gSavedPort);
  456.             }
  457.             break;
  458.         }
  459.         case kFileID:
  460.         {
  461.             ComponentResult    result = noErr;
  462.             
  463.             switch (menuItem)
  464.             {
  465.                 case kVideoSettings:
  466.                 {
  467.                     if (gSeqGrabber != 0L)
  468.                     {
  469.                         // Do the dialog thang
  470.                         result = SGSettingsDialog (gSeqGrabber, gVideoChannel, 0, 
  471.                             nil, 0L, nil, 0L);
  472.                     }
  473.                     break;
  474.                 }
  475.                 case kQuitItem:
  476.                 {
  477.                     DoQuit();
  478.                     break;
  479.                 }
  480.             }
  481.             break;
  482.         }
  483.         case kEditID:
  484.         {
  485.             if (!SystemEdit (menuItem - 1))
  486.             {
  487.                 // We don't really do anything here - feel free to implement 
  488.                 // something yourself if you want
  489.                 SysBeep(5);
  490.             }
  491.             break;
  492.         }
  493.         default:
  494.         {
  495.             break;
  496.         }
  497.     }
  498. }
  499.  
  500. //-----------------------------------------------------------------------
  501.  
  502. static void
  503. DoAboutDialog (void)
  504. {
  505.     short        itemHit;
  506.     short        itemType;
  507.     Handle        itemHandle;
  508.     Rect        itemRect;
  509.     DialogPtr    aboutDialog = GetNewDialog (kAboutDialogID, nil, (WindowPtr)-1L);
  510.  
  511.     // Do the boring about dialog
  512.     GetDItem (aboutDialog, kAboutOKButtonOutline, &itemType, &itemHandle, &itemRect);
  513.     SetDItem (aboutDialog, kAboutOKButtonOutline, itemType, 
  514.         (Handle) TesterDrawProc, &itemRect);
  515.  
  516.     ShowWindow (aboutDialog);
  517.     do
  518.     {
  519.         ModalDialog (nil, &itemHit);
  520.     }
  521.     while (itemHit != kAboutOKButton);
  522.     DisposDialog (aboutDialog);
  523. }
  524.  
  525. //-----------------------------------------------------------------------
  526.  
  527. pascal void
  528. TesterDrawProc (DialogPtr theDialog, short theItemNum)
  529. {
  530.     PenState    thePenState;
  531.     OSErr        result = noErr;
  532.     Rect        itemRect;
  533.     Handle        itemHandle;
  534.     short        itemType;
  535.     
  536.     // Set up the pen
  537.     GetPenState (&thePenState);
  538.     
  539.     GetDItem (theDialog, theItemNum, &itemType, &itemHandle, &itemRect);
  540.     
  541.     // What item do we need to draw?
  542.     switch (theItemNum)
  543.     {
  544.         case kAboutOKButtonOutline:
  545.             PenNormal();
  546.             PenMode (patCopy);
  547.             PenSize (3, 3);
  548.             InsetRect (&itemRect, -4, -4);
  549.             FrameRoundRect (&itemRect, 16, 16);
  550.             break;
  551.         default:
  552.             break;
  553.     }
  554.     
  555.     // Restore the pen
  556.     SetPenState (&thePenState);
  557. }
  558.  
  559. //-----------------------------------------------------------------------
  560.  
  561. static void
  562. DoQuit (void)
  563. {
  564.     ComponentResult    result = noErr;
  565.     
  566.     // Clean up
  567.     if (gSeqGrabber != 0L)
  568.     {
  569.         result = CloseComponent (gSeqGrabber);
  570.         gSeqGrabber = 0L;
  571.     }    
  572.     if (gMonitor != nil)
  573.     {
  574.         DisposeWindow (gMonitor);
  575.     }
  576.     
  577.     // Set quit flag
  578.     gQuitFlag = true;
  579. }
  580.  
  581. //-----------------------------------------------------------------------
  582.  
  583.